package com.chunuo.aclservice.service.impl;

import com.chunuo.aclservice.entity.Permission;
import com.chunuo.aclservice.entity.User;
import com.chunuo.aclservice.entity.security.SecurityUser;
import com.chunuo.aclservice.service.UserRoleService;
import com.chunuo.aclservice.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-08-07 15:43
 */
@Service("userDetailsService")
public class SpringDataUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionServiceImpl permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectByUsername(username);
        if(user==null){
            System.out.println("没有找到用户");
            return null;
        }
        List<String> permissionValueList = permissionService.selectPermissionValueByUserId(user.getId());
        System.out.println("user permissionValueList === "+permissionValueList);
        com.chunuo.aclservice.entity.security.User userInfo = new com.chunuo.aclservice.entity.security.User();
        BeanUtils.copyProperties(user,userInfo);

        SecurityUser securityUser = new SecurityUser(userInfo);
        securityUser.setPermissionValueList(permissionValueList);

        return securityUser;
    }
}
